import random
import os
import flickr

from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.api import urlfetch

class ImageStore(db.Model):
    image = db.BlobProperty(default=None)

class InterestingAround(webapp.RequestHandler):
    def get(self):
        """
        front = None
        back = None
        left = None
        right = None
        top = None
        bottom = None
        photos = flickr.interestingness()[:6]
        random.shuffle(photos)
        list_of_directions = []
        for photo in photos:
            farm = photo._Photo__farm
            if not farm: farm = '1'
            server = photo._Photo__server
            photoid = photo._Photo__id
            secret = photo._Photo__secret
            direction = "http://farm%s.static.flickr.com/%s/%s_%s_b.jpg" % (farm,server,photoid,secret)
            list_of_directions.append(direction)

        front, back, left, right, top, bottom = tuple(list_of_directions)

        template_values = {
                'front':front,
                'back':back,
                'left':left,
                'right':right,
                'top':top,
                'bottom':bottom
                }
        """
        template_values = {}
        path = os.path.join(os.path.dirname(__file__), 'iaround.html')
        self.response.out.write(template.render(path, template_values))

class InterestingZoomer(webapp.RequestHandler):
    def get(self):
        randnumber =random.randint(1,10)
        photo = flickr.interestingness()[randnumber]
        farm = photo._Photo__farm
        if not farm: farm = '1'
        server = photo._Photo__server
        photoid = photo._Photo__id
        secret = photo._Photo__secret
        url = "http://farm%s.static.flickr.com/%s/%s_%s_b.jpg" % (farm,server,photoid,secret)

        template_values = {
                'card': url,
                }

        path = os.path.join(os.path.dirname(__file__), 'izoomer.html')
        self.response.out.write(template.render(path, template_values))

class Yahoo(webapp.RequestHandler):
    def get(self):
        template_values = {}
        path = os.path.join(os.path.dirname(__file__), 'yahoo.html')
        self.response.out.write(template.render(path, template_values))

class Index(webapp.RequestHandler):
    def get(self):
        template_values = {}
        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, template_values))

application = webapp.WSGIApplication(
                                     [('/izoomer', InterestingZoomer),
                                      ('/iaround', InterestingAround),
                                      ('/yahoo', Yahoo),
                                      ('/',Index),],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
